package com.bbg.amyam.web.mvc.controller.views.schoolmanagementview;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.joda.time.format.DateTimeFormat;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.bbg.amyam.domain.model.SchoolAcdemicPeriod;
import com.bbg.amyam.domain.model.School;

@RequestMapping(value="/{id:[0-9a-z]{2,20}}/school_management_view/academicperiod")
@Controller
public class SchoolManagementViewAcademicperiodController {

	@RequestMapping(produces = "text/html")
	public String list(Model uiModel, HttpServletRequest httpServletRequest) {
		School currentSchool = (School) httpServletRequest
				.getAttribute("current_school_obj");
		uiModel.addAttribute("schoolacdemicperiods", SchoolAcdemicPeriod 
				.findSchoolAcdemicPeriodsBySchool(currentSchool).getResultList());
		return "school_management_view/academicperiod/index";
	}

	@RequestMapping(value = "/{id}", produces = "text/html")
	public String show(@PathVariable Long id, Model uiModel) {
		uiModel.addAttribute("schoolacdemicperiod",
				SchoolAcdemicPeriod .findSchoolAcdemicPeriod (id));
		uiModel.addAttribute("itemId", id);
		return "school_management_view/academicperiod/show";
	}

	@RequestMapping(method = RequestMethod.POST, produces = "text/html")
	public String create(@Valid SchoolAcdemicPeriod schoolAcdemicPeriod,
			BindingResult bindingResult, Model uiModel,
			HttpServletRequest httpServletRequest) {
		School currentSchool = (School) httpServletRequest
				.getAttribute("current_school_obj");
		schoolAcdemicPeriod.setSchool(currentSchool);
		uiModel.asMap().clear();
		schoolAcdemicPeriod.persist();
		return "redirect:/"+currentSchool.getShortName()+"/school_management_view/academicperiod?id=" + schoolAcdemicPeriod.getId();

	}

	@RequestMapping(params = "form", produces = "text/html")
	public String createForm(Model uiModel) {
		populateEditForm(uiModel, new SchoolAcdemicPeriod ());
		return "school_management_view/academicperiod/create";
	}

	@RequestMapping(value = "/{id}", params = "form", produces = "text/html")
	public String updateForm(@PathVariable("id") Long id, Model uiModel,
			HttpServletRequest request) {
		School currentSchool = (School) request
				.getAttribute("current_school_obj");
		SchoolAcdemicPeriod entity = SchoolAcdemicPeriod.findSchoolAcdemicPeriod(id);
		if (entity.getSchool().getId().longValue() != currentSchool.getId()
				.longValue()) {
			return "redirect:/"+currentSchool.getShortName()+"/school_management_view/academicperiod";
		}
		populateEditForm(uiModel, entity);
		return "school_management_view/academicperiod/update";
	}

	@RequestMapping(method = RequestMethod.PUT, produces = "text/html")
	public String update(@Valid SchoolAcdemicPeriod schoolAcdemicPeriod,
			BindingResult bindingResult, Model uiModel,
			HttpServletRequest httpServletRequest) {
		School currentSchool = (School) httpServletRequest
				.getAttribute("current_school_obj");
		schoolAcdemicPeriod.setSchool(currentSchool);
		uiModel.asMap().clear();
		schoolAcdemicPeriod.merge();
		return "redirect:/"+currentSchool.getShortName()+"/school_management_view/academicperiod?id=" + schoolAcdemicPeriod.getId();
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = "text/html")
	public String delete(@PathVariable("id") Long id,
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "size", required = false) Integer size,
			Model uiModel, HttpServletRequest httpServletRequest) {
		School currentSchool = (School) httpServletRequest
				.getAttribute("current_school_obj");
		SchoolAcdemicPeriod schoolAcdemicPeriod = SchoolAcdemicPeriod
				.findSchoolAcdemicPeriod(id);
		schoolAcdemicPeriod.remove();
		uiModel.asMap().clear();
		uiModel.addAttribute("page", (page == null) ? "1" : page.toString());
		uiModel.addAttribute("size", (size == null) ? "10" : size.toString());
		return "redirect:/"+currentSchool.getShortName()+"/school_management_view/academicperiod";
	}

	void populateEditForm(Model uiModel, SchoolAcdemicPeriod schoolAcdemicPeriod) {
		uiModel.addAttribute("schoolAcdemicPeriod", schoolAcdemicPeriod);
		addDateTimeFormatPatterns(uiModel);
	}
	
	void addDateTimeFormatPatterns(Model uiModel) {
		//DONT REMOVE START
uiModel.addAttribute("schoolAcdemicPeriod_openenrollenddate_date_format", DateTimeFormat.patternForStyle("S-", LocaleContextHolder.getLocale()));
uiModel.addAttribute("schoolAcdemicPeriod_openenrollstartdate_date_format", DateTimeFormat.patternForStyle("S-", LocaleContextHolder.getLocale()));
uiModel.addAttribute("schoolAcdemicPeriod_lastday_date_format", DateTimeFormat.patternForStyle("S-", LocaleContextHolder.getLocale()));
uiModel.addAttribute("schoolAcdemicPeriod_firstday_date_format", DateTimeFormat.patternForStyle("S-", LocaleContextHolder.getLocale()));
		//DONT REMOVE END 
    }

}
